﻿using System;
using System.Data;
using System.Linq;
using Model;

namespace BLL
{
    public class Information_Channel_BLL
    {
        ExtendedDBModelContainer _db = new ExtendedDBModelContainer();
        public IQueryable<Information_Channel> Get()
        {
            return _db.Information_Channel.OrderBy(a => a.lft);
        }

        public Information_Channel Get(Guid? Id)
        {
            var item = new Information_Channel();
            if (Id != null)
            {
                item = _db.Information_Channel.Single(a => a.Id.Equals(Id.Value));
            }
            return item;
        }

        public void Save(Guid? Id, ref Information_Channel item, Guid treeid)
        {
            ExtendedDBModelContainer _db = new ExtendedDBModelContainer();

            if (Id == null)
            {
                var treetop = _db.Information_Channel.Single(a => a.Id.Equals(treeid));

                item.Id = Guid.NewGuid();

                item.lvl = treetop.lvl + 1;
                item.lft = treetop.lft + 1;
                item.rgt = treetop.lft + 2;
                item.LastId = treetop.Id;

                foreach (var a in _db.Information_Channel.Where(a => a.rgt > treetop.lft))
                {
                    a.rgt = a.rgt + 2;
                }

                foreach (var a in _db.Information_Channel.Where(a => a.lft > treetop.lft))
                {
                    a.lft = a.lft + 2;
                }

                _db.Information_Channel.AddObject(item);
            }
            else
            {
                _db.Information_Channel.Attach(item);
                _db.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
            }

            _db.SaveChanges();
        }


        public void ChangeOrder(Guid Id, string direction)
        {
            var item = _db.Information_Channel.Single(a => a.Id.Equals(Id));

            //向上
            if (direction.Equals("Up"))
            {
                //查询出同级上一个是哪个
                var item1 = _db.Information_Channel.SingleOrDefault(a => a.rgt.Equals(item.lft - 1) && a.lvl.Equals(item.lvl));
                if (item1 != null)
                {
                    int chazhi = item1.rgt - item1.lft + 1;
                    foreach (var aa in _db.Information_Channel.Where(a => a.lft >= item.lft && a.rgt <= item.rgt))
                    {
                        aa.rgt = aa.rgt - chazhi;
                        aa.lft = aa.lft - chazhi;
                    }

                    int chazhi2 = item.rgt - item.lft + 1;
                    foreach (var aa in _db.Information_Channel.Where(a => a.lft >= item1.lft && a.rgt <= item1.rgt))
                    {
                        aa.rgt = aa.rgt + chazhi2;
                        aa.lft = aa.lft + chazhi2;
                    }
                }
                else
                {
                    //已经是同级第一个了
                }

            }

            //向下
            if (direction.Equals("Down"))
            {
                //查询出同级下一个是哪个
                var item1 = _db.Information_Channel.SingleOrDefault(a => a.lft == item.rgt + 1 && a.lvl == item.lvl);
                if (item1 != null)
                {
                    int chazhi = item1.rgt - item1.lft + 1;
                    foreach (var aa in _db.Information_Channel.Where(a => a.lft >= item.lft && a.rgt <= item.rgt))
                    {
                        aa.rgt = aa.rgt + chazhi;
                        aa.lft = aa.lft + chazhi;
                    }

                    int chazhi2 = item.rgt - item.lft + 1;
                    foreach (var aa in _db.Information_Channel.Where(a => a.lft >= item1.lft && a.rgt <= item1.rgt))
                    {
                        aa.rgt = aa.rgt - chazhi2;
                        aa.lft = aa.lft - chazhi2;
                    }
                }
                else
                {
                    //已经是同级最后一个了
                }
            }

            _db.SaveChanges();

        }

        public void DeleteObject(Guid Id)
        {

            var item1 = _db.Information_Channel.Single(a => a.Id.Equals(Id));

            foreach (var item in _db.Information_Channel.Where(a => a.lft >= item1.lft && a.rgt <= item1.rgt))
            {
                _db.Information_Channel.DeleteObject(item);
            }

            _db.SaveChanges();

            foreach (var a in _db.Information_Channel.Where(a => a.lft > item1.lft))
            {
                a.lft = a.lft - (item1.rgt - item1.lft + 1);
            }

            foreach (var a in _db.Information_Channel.Where(a => a.rgt > item1.lft))
            {
                a.rgt = a.rgt - (item1.rgt - item1.lft + 1);
            }

            _db.SaveChanges();
        }
    }
}
